app.controller('BusShiftInsertCtrl', function ($scope, ajax, $modal, toaster,$timeout,$filter) {

	$scope.checkbox = function () {
		var oneches = document.getElementsByName("checks");
		var ids = $scope.ids;
		for (var i = 0; i < oneches.length; i++) {
			var one = parseInt(oneches[i].value);
			if (ids.indexOf(one) != -1) {
				oneches[i].checked = true;
			}
		}
	};

	$scope.ids = [];
	$scope.checks = false;//默认未选中
	$scope.ckAll = false;//默认未选中
	$scope.choseArr = [];
	$timeout(function () {
		$scope.checkbox();
	}, 500);

    $scope.reload = $scope.page.loader('shift/bs/tpl/{status}/{pageIndex}/{pageSize}/page'.format({status:1}));
    $scope.$on('pageIndex', function(event, p) {
		$scope.reload(p, $scope.queryMap);
		$scope.checks = false;//默认未选中
		$scope.ckAll = false;//默认未选中
		$timeout(function () {
			$scope.checkbox();
		}, 500);
    });
    $scope.reload(1);



    //获取线路列表
    $scope.loadRouteList = function () {
        var promise = ajax.call(new Req.GET({
            resource: 'route/bs/list'
        }));
        promise.then(function (result) {
            $scope.routeList = result.result;
        }, function () { });
    };
    $scope.loadRouteList();

	//模糊查询
	$scope.queryMap = {};
	$scope.timeRange = {};
	$scope.query = function () {
		if ($scope.routeList.selected != undefined) {
			$scope.queryMap.route__id = $scope.routeList.selected.id;
		} else if ($scope.routeList.selected == undefined) {
			delete $scope.queryMap.route__id;
		}
        $scope.reload(1, $scope.queryMap);
		$scope.checks = false;//默认未选中
		$scope.ckAll = false;//默认未选中
		$timeout(function () {
			$scope.checkbox();
		}, 500);
	};


	/*单选*/
	$scope.check = function (val) {
		if ($scope.ids.indexOf(val) < 0) {
			$scope.ids.push(val);
		} else {
			$scope.ids.splice($scope.ids.indexOf(val), 1);
		}
	};

	/*全选*/
	$scope.checkAll = function (c, v) {
		if (c == true) {
			$scope.checks = true;
			$scope.choseArr = v;
			//添加所选Id
			for (var i = 0; i < $scope.choseArr.length; i++) {
				if($scope.ids.indexOf($scope.choseArr[i].id)>=0){
					continue;
				}else{
					$scope.ids.push($scope.choseArr[i].id);
				}
			}
		} else {
			$scope.checks = false;
			for (var i = 0; i < $scope.choseArr.length; i++) {
				$scope.ids.splice($scope.ids.indexOf($scope.choseArr[i].id),1);
			}
		}
	};

	var inserter = new Req.POST({ resource: $scope.resource });

	$scope.insert = function(){
		if($scope.ids.length == 0){
			toaster.pop('warning', '提示', '请至少勾选一个班次档案！');
			return;
		}
        var modalInstance = $modal.open({
            templateUrl: 'template/bus/shiftTemplate/insertModal.html?v=1a1fc85330',
            controller: 'BusShiftInsertModalCtrl',
            size: 'lg',
            resolve: {
                param: function() {
                    return {
                        "ids": $scope.ids
                    };
                }
            }
        });

        modalInstance.result.then(function(param) {
        	inserter.datalist=$scope.ids;
            inserter.fields = param.data;
            inserter.resource = 'shift/bs/insert';
            ajax.call(inserter).then(function (result) {
                if (result.code == 200) {
                    $scope.result = result.result;
                    toaster.pop('success', '成功', '发班操作成功，请检查班次生成情况。');
            		$modal.open({
            			templateUrl: 'template/bus/shiftTemplate/resultList.html?v=45d1cb4378',
            			controller: 'BusResultListCtrl',
            			size: 'lg',
            			resolve: {
            				param: function () { return { "list":$scope.result }; }
            			}
            		});
					$scope.ids = [];
					$scope.checks = false;//默认未选中
					$scope.ckAll = false;//默认未选中
					$scope.choseArr = [];
					$scope.reload(1, $scope.queryMap);
                } else {
                    toaster.pop('error', '错误', result.result);
                }
            }, function () {
            	toaster.pop('error', '错误', "与服务器的连接出错，发班失败！");
            });
        });
	}

});
